package com.gln.bloger.action.admin;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorActionForm;
import org.apache.struts.validator.DynaValidatorForm;

import com.gln.bloger.constant.Constant;
import com.gln.bloger.exception.AlreadyExistException;
import com.gln.bloger.pojos.Category;
import com.gln.bloger.util.PagingSupport;
import com.gln.bloger.util.StringUtil;

/**
 * Created on 20080115
 * @author renxj
 * modify entries/articles 
 */
public class CategoryAction extends UserBaseAction {
	/**
	 * save category 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
		   HttpServletRequest request, HttpServletResponse response) 
	       throws Exception{
		ActionMessages errors = form.validate(mapping, request);
		   
		   DynaValidatorActionForm categoryForm = (DynaValidatorActionForm) form;
		   String id = categoryForm.getString("id");
		   String name = categoryForm.getString("name");
		   String description = categoryForm.getString("description");
		   String secret = categoryForm.getString("secret");
		   Category category;
		   if(StringUtil.isBlank(id)){
			   //insert
			   category = new Category();
		   }else{
			   category = getCategoryService().getCategory(id);
		   }
		   category.setCreateTime(new Date());
		   category.setDescription(description);
		   category.setName(name);
		   if(Constant.CATEGORY_IS_NOT_SCERET.equals(secret)){
		       category.setSecret(Boolean.FALSE);
		   }else{
			   category.setSecret(Boolean.TRUE);
		   }
		   category.setWebsite(getAuthWebsite(request));
		   /**
		    * check if name is already exist
		    */
		   try{
			   getCategoryService().saveOrUpdateCategory(category);
			   getCategoryService().clearCachedCategories();
		   }catch(AlreadyExistException ne){
			   errors.add("errors", new 
					   ActionMessage("errors.already.exist", name));
		   }
		   
		   if(!errors.isEmpty()){
			   saveErrors(request, errors);
			   request.setAttribute("category", category);
			   return mapping.getInputForward();
		   }
		   return mapping.findForward("listCategory");
	}
	
	/**
	 * update category
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward edit(ActionMapping mapping, ActionForm form,
		   HttpServletRequest request, HttpServletResponse response) throws Exception{
		   String id = (String)request.getParameter("id");
		   if(StringUtil.isBlank(id)){
			   ActionMessages errors = new ActionMessages();
			   errors.add("erors", new ActionMessage("errors.required", "category id"));
		       saveErrors(request, errors);
			   return mapping.findForward("listCategory");
		   }
		   Category category = getCategoryService().getCategory(id);

		   request.setAttribute("category", category);
		   return mapping.findForward("categoryPage");
	}
	
	/**
	 * delete category
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward remove(ActionMapping mapping, ActionForm form,
		   HttpServletRequest request, HttpServletResponse response) throws Exception{
		   
		   DynaValidatorForm entryForm = (DynaValidatorForm)form;
		   String id = entryForm.getString("id");
		   if(StringUtil.isBlank(id)){
			   ActionMessages errors = new ActionMessages();
			   errors.add("erors", new ActionMessage("errors.required", "category id"));
		       saveErrors(request, errors);
			   return mapping.findForward("listCategory");
		   }
		   getCategoryService().removeCategory(id);
		   return mapping.findForward(Constant.SUCCESS);
	}
	/**
	 * list category
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward list(ActionMapping mapping, ActionForm form,
			   HttpServletRequest request, HttpServletResponse response) throws Exception{
			   String p = (String)request.getParameter("p");
			   PagingSupport<Category>
			   categories = getCategoryService().getAllCategories(getAuthWebsite(request));

			   if(null != p){
				   categories.setCurrentPage(Integer.parseInt(p));
			   }
			   request.setAttribute("categories", categories);
			   return mapping.findForward("listCategoryPage");
		}
	/**
	 * add a category
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
		   HttpServletRequest request, HttpServletResponse response) throws Exception{
		   return mapping.findForward("categoryPage");
	}
}
